//
//  UIViewExt.swift
//  test-ios
//
//  Created by wyz5204 on 2023/6/6.
//

import Foundation
import UIKit

enum GLBoderType {
    case top
    case bottom
    case left
    case right
}
enum GLShadowType {
    case top
    case bottom
    case left
    case right
    case noTop
    case noBottom
    case leftRight
}

extension UIView {
    /// 获取当前View 所在的控制器
    func currentVC() -> UIViewController? {
        var nextResponder: UIResponder? = self
        repeat {
            nextResponder = nextResponder?.next
            
            if let viewController = nextResponder as? UIViewController {
                return viewController
            }
            
        } while nextResponder != nil
        
        return nil
    }
    
    /// 添加任一 方向的 边框线
    func glBorder(type: GLBoderType, boderWidth: CGFloat, boderColor: UIColor, size: CGSize) {
        switch type {
        case .top:
            let layer = CALayer.init()
            layer.frame = CGRect.init(x: 0, y: 0, width: size.width, height: boderWidth)
            layer.backgroundColor = boderColor.cgColor
            self.layer.addSublayer(layer)
            break
        case .left:
            let layer = CALayer.init()
            layer.frame = CGRect.init(x: 0, y: 0, width: boderWidth, height: size.height)
            layer.backgroundColor = boderColor.cgColor
            self.layer.addSublayer(layer)
            break
        case .right:
            let layer = CALayer.init()
            layer.frame = CGRect.init(x: size.width - boderWidth, y: 0, width: boderWidth, height: size.height)
            layer.backgroundColor = boderColor.cgColor
            self.layer.addSublayer(layer)
            break
        case .bottom:
            let layer = CALayer.init()
            layer.frame = CGRect.init(x: 0, y: size.height - boderWidth, width: size.width, height: boderWidth)
            layer.backgroundColor = boderColor.cgColor
            self.layer.addSublayer(layer)
            break
        }
    }
    
    /// - Description 设置任意 角 的圆角
    /// - Parameters:
    ///   - corners: 上 下 左 右 四个角，可多选
    ///   - cornerSize: 圆角的 size
    ///   - rectSize: BezierPath 的rect  （由于snp 获取不到size）
    
    func glCornerRadius(corners: UIRectCorner, cornerSize: CGSize, rectSize: CGSize) {
        let rect = CGRect.init(x: 0, y: 0, width: rectSize.width, height: rectSize.height)
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: cornerSize)
        let cornerLayer = CAShapeLayer()
        cornerLayer.frame = self.bounds
        cornerLayer.path = path.cgPath
        self.layer.mask = cornerLayer
    }
    
    /// - Description 四个角都是圆角
    /// - Parameters:
    ///   - cornerSize: 圆角的 size
    ///   - rectSize: BezierPath 的rect  （由于snp 获取不到size）
    func glCornerRadius(size: CGSize, radius: CGFloat) {
        let rect = CGRect.init(x: 0, y: 0, width: size.width, height: size.height)
        let path = UIBezierPath.init(roundedRect: rect, cornerRadius: radius)
        let layer = CAShapeLayer.init()
        layer.frame = self.bounds
        layer.path = path.cgPath
        self.layer.mask = layer
    }
    
    /// 边框
    func glBoder(width: CGFloat, color: UIColor) {
        self.layer.borderWidth = width
        self.layer.borderColor = color.cgColor
    }
    
    func glShadow(_ radius: CGFloat = 10) {
        layer.cornerRadius = radius
        backgroundColor = .gl_white
        layer.shadowColor = UIColor.gl_shadow.cgColor
        layer.shadowOpacity = 0.4
        layer.shadowOffset = CGSize.init(width: 0, height: 5)
        layer.shadowRadius = radius
    }
    func glBlueShadow(_ radius: CGFloat = 10) {
        //        layer.cornerRadius = radius
        layer.shadowColor = UIColor.gl_theme.withAlphaComponent(0.4).cgColor
        layer.shadowOpacity = 0.4
        layer.shadowOffset = CGSize.init(width: 0, height: 5)
        layer.shadowRadius = radius
    }
    
    func glLocalShadow(type: GLShadowType, rect: CGRect, corners: UIRectCorner) {
        //        layer.masksToBounds = false
        layer.cornerRadius = 10
        backgroundColor = nil
        layer.shadowColor = UIColor.red.cgColor
        // UIColor.gl_shadow.cgColor
        layer.shadowOpacity = 0.4
        layer.shadowOffset = CGSize.init(width: 0, height: 4)
        layer.shadowRadius = 10
        let cornerLayer = CALayer()
        cornerLayer.frame = rect
        cornerLayer.backgroundColor = UIColor.gl_white.cgColor
        
        let frame = CGRect.init(x: 0, y: 0, width: rect.size.width, height: rect.size.height)
        let path = UIBezierPath(roundedRect: frame, byRoundingCorners: corners, cornerRadii: CGSize(width: 10, height: 10))
        let tempLayer = CAShapeLayer()
        tempLayer.frame = self.bounds
        tempLayer.path = path.cgPath
        cornerLayer.mask = tempLayer
        layer.addSublayer(cornerLayer)
    }
    
    var x: CGFloat! {
        set {
            self.frame.origin.x = newValue
        }
        get {
            return self.frame.origin.x
        }
    }
    var y: CGFloat! {
        set {
            self.frame.origin.y = newValue
        }
        get {
            return self.frame.origin.y
        }
    }
    var width: CGFloat! {
        set {
            self.frame.size.width = newValue
        }
        get {
            return self.frame.size.width
        }
    }
    var height: CGFloat! {
        set {
            self.frame.size.height = newValue
        }
        get {
            return self.frame.size.height
        }
    }
    var size: CGSize! {
        set {
            self.frame.size = newValue
        }
        get {
            return self.frame.size
        }
    }
}

